package com.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pet.Exception.MyCustomerException;
import com.pet.mapper.SysMenuMapper;
import com.pet.mapper.SysRoleMenuMapper;
import com.pet.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.system.SysMenu;
import com.pet.system.SysRoleMenu;
import com.pet.utils.MenuHelper;
import com.pet.utils.RouterHelper;
import com.pet.vo.AssginMenuVo;
import com.pet.vo.RouterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author whd
 * @since 2023-07-29
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> finNodes() {
        //1.获取所有菜单
        List<SysMenu> menuList=this.baseMapper.selectList(null);
        //2.所有菜单数据转换要求数据格式
        List<SysMenu> builderTree= MenuHelper.bulidTree(menuList);

        return builderTree;
    }

    @Override
    public void removeMenuById(Long id) {
        //查询当前删除菜单下面是否子菜单
        //根据id=parentid
        QueryWrapper<SysMenu> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Long count= Long.valueOf(baseMapper.selectCount(wrapper));
        if (count>0) {
            //有子菜单
            throw new MyCustomerException(201,"请先删除子菜单");
        }
        baseMapper.deleteById(id);

    }

    @Override
    public List<SysMenu> findSysMenuByRoleId(Long roleId) {
        //获取所有status为1的权限列表
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<SysMenu> menuList = baseMapper.selectList(queryWrapper);

        //根据角色id获取角色权限
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);

        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(wrapper);

        //获取该角色已分配的所有权限id
        List<Long> roleMenuIds = new ArrayList<>();
        for (SysRoleMenu roleMenu : roleMenus) {
            roleMenuIds.add(roleMenu.getMenuId());
        }
        //遍历所有权限列表
        for (SysMenu sysMenu : menuList) {
            if(roleMenuIds.contains(sysMenu.getId())){
                //设置该权限已被分配
                sysMenu.setSelect(true);
            }else {
                sysMenu.setSelect(false);
            }
        }
        //将权限列表转换为权限树
        List<SysMenu> sysMenus = MenuHelper.bulidTree(menuList);
        return sysMenus;
    }

    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        //删除已分配的权限
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id",assginMenuVo.getRoleId()));
        //遍历所有已选择的权限id
        for (Long menuId : assginMenuVo.getMenuIdList()) {
            if(menuId != null){
                //创建SysRoleMenu对象
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
                //添加新权限
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }

    @Override
    public List<RouterVo> findUserMenuList(Long userid) {
        //超级管理员admin账号id为:1
        //我们约定admin 是超级管理员拥有所有的权限
        List<SysMenu> sysMenuList=null;
        if(userid.longValue()==1){
            sysMenuList= baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status",1).orderByAsc("sort_value"));

        }else{
            //b.其他非超级管理员的用户
            sysMenuList=baseMapper.findListByUserId(userid);
        }

        List<SysMenu> sysMenusTreeList= MenuHelper.bulidTree(sysMenuList);
        //TODO MenuHelper返回的数据属性和前端路由的属性不一致，所以要处理
        //d.构建路由
        List<RouterVo> routerVoList= RouterHelper.buildRouters(sysMenusTreeList);
        return routerVoList;

    }

    @Override
    public List<String> findUserpermsList(Long id) {
        //超级管理员admin账号id为:1
        List<SysMenu> sysMenuList=null;
        if(id.longValue()==1){
            sysMenuList=this.baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status",1));
        }else {
            sysMenuList=this.baseMapper.findListByUserId(id);
        }
        //创建返回的集合
        List<String> permissionList=new ArrayList<>();
        for (SysMenu sysMenu:sysMenuList){
            if (sysMenu.getType()==2){
                permissionList.add(sysMenu.getPerms());
            }
        }
        return permissionList;
    }
}
